package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.anno.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	//声音的上传
	@PostMapping(value = "/uploadTrack")
	public Result<UploadTrackVo> uploadTrack(@RequestParam(value = "file")MultipartFile file){
		UploadTrackVo uploadTrackVo = trackInfoService.uploadTrack(file);
		return Result.ok(uploadTrackVo);
	}

	//提交音频
	@PostMapping(value = "/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo) ;
		return Result.ok() ;
	}
	//分页查询
	@GuiguLogin
	@PostMapping(value = "/findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>> findUserTrackPage(@PathVariable(value = "page") Integer pageNum ,
														@PathVariable(value = "limit") Integer pageSize ,
														@RequestBody TrackInfoQuery trackInfoQuery) {
		IPage<TrackListVo> page = trackInfoService.findUserTrackPage(pageNum , pageSize , trackInfoQuery) ;
		return Result.ok(page) ;
	}
	//根据id查询声音信息
	@GetMapping(value = "/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable(value = "id") Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo) ;
	}

	//修改声音信息
	@PutMapping(value = "/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable(value = "id") Long id , @RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(id , trackInfoVo) ;
		return Result.ok() ;
	}

	@DeleteMapping(value = "/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable(value = "id") Long id) {
		trackInfoService.removeTrackInfo(id) ;
		return Result.ok() ;
	}

	@GuiguLogin(required = false)
	@GetMapping(value = "/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId ,
															  @PathVariable(value = "page")Integer pageNo ,
															  @PathVariable(value = "limit") Integer pageSize) {
		IPage<AlbumTrackListVo> albumTrackListVoIPage = trackInfoService.findAlbumTrackPage(albumId , pageNo , pageSize) ;
		return Result.ok(albumTrackListVoIPage) ;
	}


	//获取声音播放的统计数据,以便于后续的排行
	//因为经过声音播放之后,播放量会改变
	@GetMapping(value = "/getTrackStatVo/{trackId}")
	public Result<TrackStatVo>  getTrackStatVo(@PathVariable(value = "trackId") Long trackId) {
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId) ;
		return Result.ok(trackStatVo) ;
	}

	@GuiguLogin
	@GetMapping(value = "/findUserTrackPaidList/{trackId}")
	public Result<List<TrackPayVo>> findUserTrackPaidList(@PathVariable(value = "trackId") Long trackId) {
		List<TrackPayVo> trackPayVoList = trackInfoService.findUserTrackPaidList(trackId) ;
		return Result.ok(trackPayVoList) ;
	}

	// 获取用户要购买的声音的列表
	@GetMapping(value = "/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable(value = "trackId") Long trackId , @PathVariable(value = "trackCount") Integer trackCount) {
		List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(trackId , trackCount) ;
		return Result.ok(list) ;
	}
}

